// index.js
// 获取应用实例
const app = getApp()

Page({
  data: {
    listData: [],
    testData: {
      "reason": "查询成功!",
      "result": {
        "title": "美国男子职业篮球联赛",
        "duration": "2020-2021",
        "matchs": [{
            "date": "2021-05-15",
            "week": "周六",
            "list": [{
                "time_start": "07:00",
                "status": "3",
                "status_text": "完赛",
                "team1": "波士顿凯尔特人",
                "team2": "密尔沃基雄鹿",
                "team1_score": "120",
                "team2_score": "105"
              },
              {
                "time_start": "08:00",
                "status": "3",
                "status_text": "完赛",
                "team1": "迈阿密热火",
                "team2": "金州勇士",
                "team1_score": "93",
                "team2_score": "109"
              },

            ]
          },
          {
            "date": "2021-05-16",
            "week": "周日",
            "list": [{
                "time_start": "01:00",
                "status": "3",
                "status_text": "完赛",
                "team1": "波士顿凯尔特人",
                "team2": "密尔沃基雄鹿",
                "team1_score": "105",
                "team2_score": "91"
              },
              {
                "time_start": "01:00",
                "status": "3",
                "status_text": "完赛",
                "team1": "迈阿密热火",
                "team2": "金州勇士",
                "team1_score": "115",
                "team2_score": "122"
              },

            ]
          },
        ]
      },
      "error_code": 0
    }
  },

  onLoad() {
    let that = this;

    //加载本地测试数据
    // that.setData({
    //   listData: that.remapData(that.data.testData.result.matchs)
    // })
    // console.log(that.data.listData);

    //加载api数据
    wx.request({
      url: 'http://apis.juhe.cn/fapig/nba/query',
      header:{
        'content-type':'application/json'
      },
      data:{
        key:'ec87b622e4628736911f310976e981d2'
      },
      success(res){
        console.log(res.data);
        that.setData({
          listData:that.remapData(res.data.result.matchs)
        })
      }
    })
  },


  //整理从接口获取到的数据
  remapData: function (data) {
    console.log("enter function");
    let listData = [];
    for (let i = 0; i < data.length; i++) {
      let listMatchs = [];
      for (let j = 0; j < data[i].list.length; j++) {
        const item = data[i].list[j];
        console.log("enter loop");
        item.teamicon1 = this.getTeamIcon(item.team1);
        item.teamicon2 = this.getTeamIcon(item.team2);
        listMatchs.push(item);
      }
        const weekItem = data[i].week;
      let arr={
        "weekItem":weekItem,
        "matchs":listMatchs
      };
      listData.push(arr);
    }
    return listData;
  },

  //根据接口返回的队伍名称来获取相应队伍的队标图片地址
  getTeamIcon: function (teamName) {
    switch (teamName) {
      case "费城76人":
        return "/images/76ers.png";
      case "迈阿密热火":
        return "/images/Heat.png";
      case "波士顿凯尔特人":
        return "/images/celtics.png";
      case "金州勇士":
        return "/images/gold.png";
      case "孟菲斯灰熊":
        return "/images/mph.png";
      case "密尔沃基雄鹿":
        return "/images/mil.png";
      case "达拉斯独行侠":
        return "/images/mavericks.png";
      case "菲尼克斯太阳":
        return "/images/sun.png";
    }
  },

  //跳转到详情页，并传递获取到的队伍信息到详情页
  goToDetail: function (e) {
    let team1 = e.currentTarget.dataset.team1;
    let team2 = e.currentTarget.dataset.team2;
    wx.navigateTo({
      url: '/pages/detail/detail?team1=' + team1 + '&team2=' + team2,
    })
  }
})